package com.slipper.system.collect.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.util.Tool;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.collect.dao.CollectTableDao;
import com.slipper.system.collect.bo.CollectTableBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class CollectTableService {
    @Resource
    private CollectTableDao collectTableDao;
    @Resource
    private CollectColumnService collectColumnService;
    
    public CollectTableBO queryBy(String id) {
        return collectTableDao.queryBy(id);
    }
    
    public List<CollectTableBO> queryList(CollectTableBO bo) {
        return collectTableDao.queryList(bo);
    }
    
    public PageInfo<CollectTableBO> queryPage(CollectTableBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(collectTableDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(CollectTableBO bo) {
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            return collectTableDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            return collectTableDao.insert(bo);
        }
    }

    @WriteTransactional
    public int save(String taskId, List<CollectTableBO> list, String employeeId) {
        if (Tool.isBlank(taskId)) {
            return 0;
        }

        // 保存/更新数据
        for (CollectTableBO table : list) {
            table.setTaskId(taskId);
            if (Tool.isBlank(table.getId())) {
                table.setId(UUID.getUUID());
                table.setUpdateId(employeeId);
                table.setCreateId(employeeId);
                table.setUpdateTime(System.currentTimeMillis());
                table.setCreateTime(System.currentTimeMillis());
                collectTableDao.insert(table);
            } else {
                table.setUpdateId(employeeId);
                table.setUpdateTime(System.currentTimeMillis());
                collectTableDao.update(table);
            }
            collectColumnService.save(table.getId(), table.getColumnList(), employeeId);
        }

        // 删除被删除的数据
        CollectTableBO qo = new CollectTableBO();
        qo.setTaskId(taskId);
        collectTableDao.queryList(qo).stream()
                .filter(table -> list.stream().noneMatch(temp -> table.getId().equals(temp.getId())))
                .forEach(bo -> {
                    collectTableDao.delete(bo);
                    collectColumnService.deleteByTableId(bo.getId());
                });
        return 1;
    }
    
    @WriteTransactional
    public int delete(CollectTableBO bo) {
        return collectTableDao.delete(bo);
    }

    @WriteTransactional
    public void deleteByTaskId(String taskId) {
        if (Tool.isBlank(taskId)) return;
        CollectTableBO qo = new CollectTableBO();
        qo.setTaskId(taskId);
        collectTableDao.queryList(qo).forEach(bo -> {
            collectTableDao.delete(bo);
            collectColumnService.deleteByTableId(bo.getId());
        });
    }
}
